﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestOnlineProject.Models.DataAccess;
using TestOnlineProject.Models.Entities;

namespace TestOnlineProject.Models.BusinessLogic
{
    public class UserTestBLO
    {
        UserTestDAO userTestDAO = new UserTestDAO();
        UserDAO userDAO = new UserDAO();

        public List<UserTest> ToList(int testId)
        {
            try
            {
                var user = userDAO.SelectByUserId(int.Parse(HttpContext.Current.User.Identity.Name));
                List<UserTest> userTests;

                // Admin and teacher role.
                userTests = userTestDAO.SelectByTestId(testId).OrderByDescending(ut => ut.UserTestId).ToList();

                return userTests;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public UserTest Find(int userTestId)
        {
            try
            {
                var userTest = userTestDAO.SelectByUserTestId(userTestId);
                return userTest;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Close(int userId)
        {
            try
            {
                var result = 0;
                var userTests = userTestDAO.SelectByUserId(userId);
                foreach (var u in userTests)
                {
                    result += userTestDAO.Update(u);
                }
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int CheckIsUnfinishedTest(int userId)
        {
            try
            {
                var userTests = userTestDAO.SelectByUserId(userId);

                foreach (var i in userTests)
                {
                    if (DateTime.Now < i.StartTime.AddMinutes(i.Test.Duration) && i.FinishTime == null)
                    {
                        return i.UserTestId;
                    }
                }
                return 0;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int CheckIsUnfinishedTest(int userId, int testTypeId)
        {
            try
            {
                var userTests = userTestDAO.SelectByUserId(userId);
                foreach (var userTest in userTests)
                {
                    if(userTest.FinishTime == null)
                    {
                        return userTest.UserTestId;
                    }
                }
                return 0;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public UserTest AddQuickTest(int userId)
        {
            try
            {
                var userTest = new UserTest
                    {
                        UserId = userId,
                        StartTime = DateTime.Now,
                        Mark = 0
                    };
                var result = userTestDAO.Insert(userTest);
                return result > 0 ? userTest : null;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public int Modify (int userTestId, double grade)
        {
            try
            {
                // Select Discussion with discussionId
                var userTest = userTestDAO.SelectByUserTestId(userTestId);

                // Modify Discussion
                userTest.Mark += grade;

                // Update to Database
                var result = userTestDAO.Update(userTest, "NotFinish");
                return result;
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }
}